package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.Constant;
import cn.zysuyuan.common.entity.DataResult;
import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.common.utils.UUIDUtil;
import cn.zysuyuan.item.entity.DeviceData;
import cn.zysuyuan.item.service.DeviceDataService;
import cn.zysuyuan.item.utils.CaculateData;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Response;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019/10/28 12:54
 */

@Api(tags = "设备数据管理")
@RestController
@RequestMapping("devicedata")
public class DeviceDataController {

    @Autowired
    private DeviceDataService deviceDataService;

    @ApiOperation(value = "测试连接", notes = "测试连接")
    @GetMapping("connect")
    public Map<String,Object> getConnect() {
        Map<String,Object> map = new HashMap<>();
        map.put("connect","success");
        return map;
    }

    @ApiOperation(value = "设备数据", notes = "列表查询")
    @GetMapping("list")
    public ResponseEntity<PageResult<DeviceData>> queryDeviceDataByPage(@RequestParam Map<String,Object> map){

        PageResult<DeviceData> result = this.deviceDataService.selectDeviceDataByPage(map);
//        if (CollectionUtils.isEmpty(result.getRows())){
//            return ResponseEntity.notFound().build();
//        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "查询最新的环境数据", notes = "查询最新的环境数据")
    @GetMapping("newData")
    public ResponseEntity<List<DeviceData>> selectNewData() {
        List<DeviceData> list = deviceDataService.selectNewOne();
        return ResponseEntity.ok(list);
    }

    @ApiOperation(value = "查询特定的环境数据", notes = "查询特定的环境数据")
    @GetMapping("eachDeviceData")
    public ResponseEntity<Map<String,Object>> selectEachDeviceData(@RequestParam("deviceName") String name) {
//        Map<String, Object> maps = deviceDataService.selectEachDeviceData(name);
        Map<String,Object> maps = deviceDataService.getEchartsData(name);
        return ResponseEntity.ok(maps);
    }

    @ApiOperation(value = "删除异常数据", notes = "删除异常数据")
    @DeleteMapping("warning")
    public R deleteWarningData(@RequestParam("id") String id) {
        deviceDataService.deleteById(id);
        return R.ok("删除数据成功");
    }

    @ApiOperation(value = "接收上位机数据", notes = "接收上位机数据")
    @PostMapping("save")
    public DataResult save(HttpServletRequest request) {

        DeviceData sensorDevices = new DeviceData();
        JSONObject json = new JSONObject();
        BufferedReader br;
        String line = null;
        try {
            br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream()));
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            json = JSONObject.parseObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        sensorDevices.setId(UUIDUtil.uuid());
        sensorDevices.settSerialNumber(json.getString("tSerialNumber"));
        sensorDevices.settTemperature((json.getDouble("tTemperature") == null) ? 0 :json.getDouble("tTemperature"));
        sensorDevices.settHumidity((json.getDouble("tHumidity") == null) ? 0 : json.getDouble("tHumidity"));
        sensorDevices.settLightIntensity((json.getDouble("tLightIntensity") == null) ? 0 : json.getDouble("tLightIntensity"));
        sensorDevices.settSoilTemperature01((json.getDouble("tSoilTemperature01") == null) ? 0 :json.getDouble("tSoilTemperature01"));
        sensorDevices.settSoilTemperature02((json.getDouble("tSoilTemperature02") == null) ? 0 :json.getDouble("tSoilTemperature02"));
        sensorDevices.settSoilTemperature03((json.getDouble("tSoilTemperature03") == null) ? 0 :json.getDouble("tSoilTemperature03"));
        sensorDevices.settSoilTemperature04((json.getDouble("tSoilTemperature04") == null) ? 0 :json.getDouble("tSoilTemperature04"));
        sensorDevices.settSoilTemperature05((json.getDouble("tSoilTemperature05") == null) ? 0 :json.getDouble("tSoilTemperature05"));
        sensorDevices.settSoilTemperature06((json.getDouble("tSoilTemperature06") == null) ? 0 :json.getDouble("tSoilTemperature06"));
        sensorDevices.settSoilTemperature07((json.getDouble("tSoilTemperature07") == null) ? 0 :json.getDouble("tSoilTemperature07"));
        sensorDevices.settSoilTemperature08((json.getDouble("tSoilTemperature08") == null) ? 0 :json.getDouble("tSoilTemperature08"));
        sensorDevices.settSoilTemperature09((json.getDouble("tSoilTemperature09") == null) ? 0 :json.getDouble("tSoilTemperature09"));
        sensorDevices.settSoilTemperature(CaculateData.CaculateData2(sensorDevices));
        sensorDevices.settSoilHumidity01((json.getDouble("tSoilHumidity01") == null) ? 0 : json.getDouble("tSoilHumidity01"));
        sensorDevices.settSoilHumidity02((json.getDouble("tSoilHumidity02") == null) ? 0 : json.getDouble("tSoilHumidity02"));
        sensorDevices.settSoilHumidity03((json.getDouble("tSoilHumidity03") == null) ? 0 : json.getDouble("tSoilHumidity03"));
        sensorDevices.settSoilHumidity04((json.getDouble("tSoilHumidity04") == null) ? 0 : json.getDouble("tSoilHumidity04"));
        sensorDevices.settSoilHumidity05((json.getDouble("tSoilHumidity05") == null) ? 0 : json.getDouble("tSoilHumidity05"));
        sensorDevices.settSoilHumidity06((json.getDouble("tSoilHumidity06") == null) ? 0 : json.getDouble("tSoilHumidity06"));
        sensorDevices.settSoilHumidity07((json.getDouble("tSoilHumidity07") == null) ? 0 : json.getDouble("tSoilHumidity07"));
        sensorDevices.settSoilHumidity08((json.getDouble("tSoilHumidity08") == null) ? 0 : json.getDouble("tSoilHumidity08"));
        sensorDevices.settSoilHumidity09((json.getDouble("tSoilHumidity09") == null) ? 0 : json.getDouble("tSoilHumidity09"));
        sensorDevices.settSoilHumidity(CaculateData.CaculateData1(sensorDevices));
        sensorDevices.settSoilPh((json.getDouble("tSoilPh") == null) ? 0 : json.getDouble("tSoilPh"));
        sensorDevices.settCreateTime(new Date());

        System.out.println("接收数据："+ sensorDevices.toString());
        DataResult dataResult = new DataResult();
        dataResult.setStatus(Constant.SUCCEED_CODE);
        dataResult.setData(deviceDataService.saveInfo(sensorDevices));

        return dataResult;
    }
}
